package com.yyy.dm.config;

import lombok.Getter;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@Getter
public class RabbitMQConfig {

    @Value("${dm.order.exchange}")
    private String order_exchange;
    @Value("${dm.order.queue}")
    private String order_queue;
    @Value("${dm.order.routingKey}")
    private String order_routing_key;
    @Value("${dm.dlx.exchange}")
    private String order_dlx_exchange;
    @Value("${dm.dlx.queue}")
    private String order_dlx_queue;
    @Value("${dm.dlx.routingKey}")
    private String order_dlx_routing_key;


    //创建业务交换器
    @Bean
    public DirectExchange orderExchange(){
        return new DirectExchange(order_exchange);
    }

    //死信队列交换器
    @Bean
    public DirectExchange orderDlxExchange(){
        return new DirectExchange(order_dlx_exchange);
    }

    //普通订单队列
    @Bean
    public Queue orderQueue(){
        Map<String,Object> param = new HashMap<>();
        param.put("x-dead-letter-exchange",order_dlx_exchange);
        param.put("x-dead-letter-routing-key",order_dlx_routing_key);
        return new Queue(order_queue,true,false,false,param);
    }

    @Bean
    public Queue orderDlxQueue(){
        return new Queue(order_dlx_queue,true,false,false);
    }

    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(orderQueue())
                .to(orderExchange())
                .with(order_routing_key);
    }

    @Bean
    public Binding orderDlxBinding(){
        return BindingBuilder.bind(orderDlxQueue())
                .to(orderDlxExchange())
                .with(order_dlx_routing_key);
    }

}
